package com.dylan.历史;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: ankang
 * @Date: 2019/1/16 18:35
 * @Description:
 */
public class Test22 {

    public static void main(String[] args) {
        System.out.println(spiralOrder(new int[][]{{1,2,3},{4,5,6},{7,8,9}}));
    }

    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix.length != 0) {
            int row = matrix.length;
            int column = matrix[0].length;
            spiralOrderDo(result, matrix, 0, column-1, 0, row-1, 0, 0);
        }
        return result;
    }

    public static void spiralOrderDo(List<Integer> result, int[][] matrix, int left, int right, int up, int down, int row, int column) {
        boolean isLeft = left == column;
        boolean isRight = right == column;
        boolean isUp = up == row;
        boolean isDown = down == row;
        if (isLeft && isUp) {
            for(int i=left;i<=right;i++) {
                result.add(matrix[up][i]);
            }
            up = up + 1;
            row = up;
            column = right;
        }
        if (isRight && isUp) {
            for(int i=up;i<=down;i++) {
                result.add(matrix[i][right]);
            }
            right = right - 1;
            row = down;
            column = right;
        }
        if (isRight && isDown) {
            for(int i=right;i>=left;i--) {
                result.add(matrix[down][i]);
            }
            down = down - 1;
            row = down;
            column = left;
        }
        if (isLeft && isDown) {
            for(int i=down;i>=up;i--) {
                result.add(matrix[i][left]);
            }
            left = left + 1;
            row = up;
            column = left;
        }
        if(left <= right && up <= down) {
            spiralOrderDo(result, matrix, left, right, up, down, row, column);
        }
    }
}
